const axios = require('axios');

const BASE_URL = 'http://localhost:8085';

async function testCallbacks() {
    try {
        console.log('Testing SRS Callback API...\n');

        // Test on_publish callback
        console.log('1. Testing on_publish callback...');
        const publishData = {
            action: 'on_publish',
            client_id: 'test-client-123',
            ip: '127.0.0.1',
            vhost: '__defaultVhost__',
            app: 'live',
            stream: 'teststream',
            param: '?token=test123',
            server_id: 'test-server-456',
            stream_url: 'test.com/live/teststream',
            stream_id: 'test-stream-789',
            tcUrl: 'rtmp://127.0.0.1:1935/live'
        };

        const publishResponse = await axios.post(`${BASE_URL}/api/v1/streams`, publishData);
        console.log('✅ on_publish response:', publishResponse.data);

        // Test on_play callback
        console.log('\n2. Testing on_play callback...');
        const playData = {
            action: 'on_play',
            client_id: 'test-player-123',
            ip: '127.0.0.1',
            vhost: '__defaultVhost__',
            app: 'live',
            stream: 'teststream',
            param: '?token=test123',
            server_id: 'test-server-456',
            stream_url: 'test.com/live/teststream',
            stream_id: 'test-stream-789',
            pageUrl: 'http://test.com/player.html'
        };

        const playResponse = await axios.post(`${BASE_URL}/api/v1/sessions`, playData);
        console.log('✅ on_play response:', playResponse.data);

        // Test heartbeat
        console.log('\n3. Testing heartbeat...');
        const heartbeatData = {
            device_id: 'test-device-123',
            ip: '192.168.1.100',
            summaries: {
                self: {
                    pid: 12345,
                    srs_uptime: 3600
                }
            }
        };

        const heartbeatResponse = await axios.post(`${BASE_URL}/api/v1/servers`, heartbeatData);
        console.log('✅ heartbeat response:', heartbeatResponse.data);

        // Test health check
        console.log('\n4. Testing health check...');
        const healthResponse = await axios.get(`${BASE_URL}/health`);
        console.log('✅ health check response:', healthResponse.data);

        // Get stored callbacks
        console.log('\n5. Fetching stored callbacks...');
        const callbacksResponse = await axios.get(`${BASE_URL}/api/v1/callbacks`);
        console.log('✅ Stored callbacks:', callbacksResponse.data.length, 'records');

        // Get stored heartbeats
        console.log('\n6. Fetching stored heartbeats...');
        const heartbeatsResponse = await axios.get(`${BASE_URL}/api/v1/heartbeats`);
        console.log('✅ Stored heartbeats:', heartbeatsResponse.data.length, 'records');

        console.log('\n🎉 All tests passed!');

    } catch (error) {
        console.error('❌ Test failed:', error.response?.data || error.message);
    }
}

// Run tests
testCallbacks(); 